package com.yjt.fresh.adpater;

import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yjt.fresh.R;
import com.yjt.fresh.activity.AtyOrderDetail;
import com.yjt.fresh.bean.BeanOrder;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.fragment.order.FrgOrderBase;

import java.util.List;

/**
 * Created by UPC on 2018/8/6.
 */

public class AdpRvMyOrder extends BaseQuickAdapter<BeanOrder, BaseViewHolder> {

    private final FrgOrderBase mFrgOrderBase;

    public AdpRvMyOrder(int layoutResId, @Nullable List<BeanOrder> data, FrgOrderBase frgOrderBase) {
        super(layoutResId, data);
        mFrgOrderBase = frgOrderBase;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final BeanOrder item) {
        RecyclerView rvChild = helper.getView(R.id.rvAppRecycleItemAtyMyOrder);
        LinearLayout llSingleGoods = helper.getView(R.id.llAppRecycleItemAtyMyOrderSingleGoods);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvChild.setLayoutManager(manager);
        AdpRvMyOrderChild adpRvMyOrderChild = new AdpRvMyOrderChild(R.layout.app_recycle_item_frg_my_order_child, item.getGOODS());
        rvChild.setAdapter(adpRvMyOrderChild);

        if (item.getGOODS().size() <= 1){
            rvChild.setVisibility(View.GONE);
            llSingleGoods.setVisibility(View.VISIBLE);
            ImageView iv = helper.getView(R.id.ivAppRecycleItemAtyMyOrderSingleGoods);
            TextView tvTitle = helper.getView(R.id.tvAppRecycleItemAtyMyOrderSingleGoodsTitle);
            TextView tvType = helper.getView(R.id.tvAppRecycleItemAtyMyOrderSingleGoodsType);
            Glide.with(mContext).load(item.getGOODS().get(0).getSKU_IMG()).into(iv);
            tvTitle.setText(item.getGOODS().get(0).getTITLE());
            tvType.setText(item.getGOODS().get(0).getSIZE());
        }else {
            rvChild.setVisibility(View.VISIBLE);
            llSingleGoods.setVisibility(View.GONE);
        }

        TextView tvOrderNum = helper.getView(R.id.tvAppRecycleItemAtyMyOrderOrderNum);
        TextView tvOrderStatus = helper.getView(R.id.tvAppRecycleItemAtyMyOrderStatus);
        TextView tvOrderGoodsCount = helper.getView(R.id.tvAppRecycleItemAtyMyOrderGoodsCount);
        TextView tvPrice = helper.getView(R.id.tvAppRecycleItemAtyMyOrderPrice);

        TextView tvService = helper.getView(R.id.tvAppRecycleItemAtyMyOrderContactService);//联系客服
        TextView tvCancelOrder = helper.getView(R.id.tvAppRecycleItemAtyMyOrderCancelOrder);//取消订单
        TextView tvRemindSend = helper.getView(R.id.tvAppRecycleItemAtyMyOrderNoticeSend);//提醒发货
        TextView tvCheckExpress = helper.getView(R.id.tvAppRecycleItemAtyMyOrderCheckExpress);//查看物流
        TextView tvConfirmReceive = helper.getView(R.id.tvAppRecycleItemAtyMyOrderConfirmReceive);//确认收货
        TextView tvAfterSale = helper.getView(R.id.tvAppRecycleItemAtyMyOrderService);//售后
        TextView tvComment = helper.getView(R.id.tvAppRecycleItemAtyMyOrderComment);//评价
        TextView tvDeleteOrder = helper.getView(R.id.tvAppRecycleItemAtyMyOrderDeleteOrder);//删除订单
        TextView tvPay = helper.getView(R.id.tvAppRecycleItemAtyMyOrderPay);//支付
        TextView tvAfterSaleAbove = helper.getView(R.id.tvAppRecycleItemAtyMyOrderServiceAbove);//售后上面的

        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderContactService);
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderCancelOrder);
        if ("1".equals(item.getISREMIND())){
            tvRemindSend.setText("已提醒");
            tvRemindSend.setClickable(false);
        }else {
            tvRemindSend.setClickable(true);
            helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderNoticeSend);
        }
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderCheckExpress);
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderConfirmReceive);
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderService);
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderComment);
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderDeleteOrder);
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderPay);
        helper.addOnClickListener(R.id.tvAppRecycleItemAtyMyOrderServiceAbove);

        rvChild.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    Intent intent = new Intent(mContext, AtyOrderDetail.class);
                    intent.putExtra(MyConstants.ORDER_ID, item.getORDERS_ID());
                    mFrgOrderBase.startActivityForResult(intent, FrgOrderBase.REQUEST_CODE);
                }
                return false;
            }
        });
        tvOrderGoodsCount.setText("共"+item.getGOODSCOUNT()+"件商品  实付款：");
        tvOrderNum.setText("订单号:  " + item.getORDERS_ID());
        tvPrice.setText("￥"+item.getORDERMONEY());
        tvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.color_43b624));
        switch (item.getSTATUS()) {
            case 0://待付款
                tvOrderStatus.setText("待付款");

                tvService.setVisibility(View.VISIBLE);
                tvPay.setVisibility(View.VISIBLE);
                tvCancelOrder.setVisibility(View.VISIBLE);

                tvRemindSend.setVisibility(View.GONE);
                tvCheckExpress.setVisibility(View.GONE);
                tvConfirmReceive.setVisibility(View.GONE);
                tvAfterSale.setVisibility(View.GONE);
                tvComment.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);


                break;
            case 1://待发货
                tvOrderStatus.setText("待发货");
                tvService.setVisibility(View.VISIBLE);
                tvRemindSend.setVisibility(View.VISIBLE);
                tvCancelOrder.setVisibility(View.VISIBLE);
                tvAfterSale.setVisibility(View.GONE);
                tvCheckExpress.setVisibility(View.GONE);
                tvConfirmReceive.setVisibility(View.GONE);
                tvComment.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvPay.setVisibility(View.GONE);
                break;
            case 2://已发货
                tvOrderStatus.setText("已发货");
                tvService.setVisibility(View.VISIBLE);
                tvCheckExpress.setVisibility(View.VISIBLE);
                tvConfirmReceive.setVisibility(View.VISIBLE);

                tvRemindSend.setVisibility(View.GONE);
                tvCancelOrder.setVisibility(View.GONE);
                tvAfterSale.setVisibility(View.GONE);
                tvComment.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvPay.setVisibility(View.GONE);
                break;
            case 3://已完成
                tvOrderStatus.setText("已完成");
                tvDeleteOrder.setVisibility(View.VISIBLE);

                tvAfterSale.setVisibility(View.GONE);
                tvService.setVisibility(View.GONE);
                tvCheckExpress.setVisibility(View.GONE);
                tvConfirmReceive.setVisibility(View.GONE);
                tvRemindSend.setVisibility(View.GONE);
                tvCancelOrder.setVisibility(View.GONE);
                tvComment.setVisibility(View.GONE);
                tvPay.setVisibility(View.GONE);
                break;
            case 4://已取消
                tvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.color_999999));
                tvOrderStatus.setText("已取消");
                tvDeleteOrder.setVisibility(View.VISIBLE);

                tvAfterSale.setVisibility(View.GONE);
                tvService.setVisibility(View.GONE);
                tvCheckExpress.setVisibility(View.GONE);
                tvConfirmReceive.setVisibility(View.GONE);
                tvRemindSend.setVisibility(View.GONE);
                tvCancelOrder.setVisibility(View.GONE);
                tvComment.setVisibility(View.GONE);
                tvPay.setVisibility(View.GONE);

                break;
            case 5://待评价
                tvOrderStatus.setText("待评价");
                tvComment.setVisibility(View.VISIBLE);
                tvAfterSaleAbove.setVisibility(View.VISIBLE);

                tvAfterSale.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvService.setVisibility(View.GONE);
                tvCheckExpress.setVisibility(View.GONE);
                tvConfirmReceive.setVisibility(View.GONE);
                tvRemindSend.setVisibility(View.GONE);
                tvCancelOrder.setVisibility(View.GONE);
                tvPay.setVisibility(View.GONE);
                break;
            case 6:
                tvOrderStatus.setText("待收货");
                tvService.setVisibility(View.VISIBLE);
                tvCheckExpress.setVisibility(View.VISIBLE);
                tvConfirmReceive.setVisibility(View.VISIBLE);
                tvAfterSale.setVisibility(View.VISIBLE);

                tvRemindSend.setVisibility(View.GONE);
                tvCancelOrder.setVisibility(View.GONE);
                tvComment.setVisibility(View.GONE);
                tvDeleteOrder.setVisibility(View.GONE);
                tvPay.setVisibility(View.GONE);
                break;
        }

    }
}
